export let utils = {
  isIphone: function () {
    return /iphone/gi.test(window.navigator.userAgent)
  },
  /**
   * 是否特殊机型
   * @returns boolean
   */
  isIphoneX: function () {
    if (window !== 'undefined' && window && !utils.isIphone()) return false

    const XConfig = [
      // Iphone Xs (Max, PRo Max)
      {
        devicePixelRatio: 3,
        width: 414,
        height: 896,
      },
      // Iphone XR (11)
      {
        devicePixelRatio: 2,
        width: 414,
        height: 896,
      },
      // Iphone X (Xs, Pro)
      {
        devicePixelRatio: 3,
        width: 375,
        height: 812,
      },
    ]

    const {
      devicePixelRatio,
      screen: { width, height },
    } = window

    return XConfig.some((it) => it.devicePixelRatio === devicePixelRatio && it.width === width && it.height === height)
  },

  /**
   * base64转码（压缩完成后的图片为base64编码，这个方法可以将base64编码转回file文件）
   */
  dataURLtoFile: function (dataurl, filename) {
    var arr = dataurl.split(',')
    var mime = arr[0].match(/:(.*?)/)[1]
    var bstr = atob(arr[1])
    var n = bstr.length
    var u8arr = new Uint8Array(n)
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n)
    }
    return new File([u8arr], filename, { type: mime })
  },
  /**
   * js 除法计算   防止精度丢失
   * @params {number} a
   * @params {number} b
   *
   * @returns {string}
   */
  accDiv: function (arg1, arg2) {
    var t1 = 0
    var t2 = 0
    var r1
    var r2
    try {
      t1 = arg1.toString().split('.')[1].length
    } catch (e) {
      console.log(e)
    }
    try {
      t2 = arg2.toString().split('.')[1].length
    } catch (e) {
      console.log(e)
    }
    r1 = Number(arg1.toString().replace('.', ''))
    r2 = Number(arg2.toString().replace('.', ''))
    return (r1 / r2) * Math.pow(10, t2 - t1)
  },

  toast: function (msg, duration) {
    duration = isNaN(duration) ? 2000 : duration
    var m = document.createElement('div')
    m.innerHTML = msg
    m.style.cssText = 'font-size: 10px;color: black;box-sizing: border-box;background-color: rgba(255, 255, 255, 1);padding: 10px 5px;border-radius: 4px;position: fixed;top: 80%;left:calc((100vw - 170px) / 2);width: 170px;text-align: center;z-index: 10000;line-height: 20px;letter-spacing: 1px;'
    document.body.appendChild(m)
    setTimeout(function () {
      var d = 0.5
      m.style.opacity = '0'
      setTimeout(function () {
        document.body.removeChild(m)
      }, d * 1000)
    }, duration)
  },
  // 横屏提示
  toast_horizontal: function (msg, duration) {
    duration = isNaN(duration) ? 2000 : duration
    var m = document.createElement('div')
    m.innerHTML = msg
    m.style.cssText = `
    font-size: 12px;
    color: white;
    box-sizing: border-box;
    background-color: black;
    padding: 8px 15px;
    border-radius: 10px;
    position: fixed;
    top: 50%;
    right: 0px;
    text-align: center;
    z-index: 10000;
    line-height: 20px;
    letter-spacing: 1px;
    `
    // transform:  rotate(90deg);
    // transform-origin:50% 50%;
    document.body.appendChild(m)
    setTimeout(function () {
      var d = 0.5
      m.style.opacity = '0'
      setTimeout(function () {
        document.body.removeChild(m)
      }, d * 1000)
    }, duration)
  },
}
